package com.sec.android.app.myfiles.module.shortcut;

import android.text.TextUtils;
import android.view.DragEvent;
import android.view.View;
import android.widget.AbsListView;

import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.AbsDragAndDrop;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;

/**
 * Created by yunju.cha on 2017-07-20.
 */

public class ShortcutDragAndDropImp extends AbsDragAndDrop {
    private AbsFileListAdapterImp mAdapter;
    public ShortcutDragAndDropImp(AbsMyFilesFragment fragment, AbsListViewImp listViewImp) {
        super(fragment, listViewImp);
    }

    @Override
    protected boolean handleActionDragStarted(DragEvent event, AbsListView listView) {
        mAdapter = (AbsFileListAdapterImp) mFragment.getListView().getAdapter();
        if (event.getClipDescription() != null) {
            if (!TextUtils.isEmpty(event.getClipDescription().getLabel())) {
                UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_DRAG_AND_DROP_START, -1);
            }
        }
        return true;
    }

    @Override
    protected boolean handleActionDragLocation(View view, DragEvent event, AbsListView listView, int position, int y) {
        clearBackground(position, view);
        if (position != AppConstants.UNDEFINED) {
            listView.getChildAt(position).setPressed(true);
            listView.getChildAt(position).setBackgroundResource(UiUtils.getSelectableItemBackground(mContext));
        }
        mPrevPosition = position;
        return true;
    }

    @Override
    protected void handleActionDragExited(View view, int position) {
        clearBackground(AppConstants.UNDEFINED, view);
        mPrevPosition = AppConstants.UNDEFINED;
    }

    @Override
    protected void handleActionDragEnded(View view, DragEvent event, AbsListView listView, int position) {
        clearBackground(AppConstants.UNDEFINED, view);
        UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_DRAG_AND_DROP_END, -1);
        mPrevPosition = AppConstants.UNDEFINED;
    }

    @Override
    protected boolean handleActionDrop(View view, DragEvent event, int position) {
        boolean bRet;
        mFragment.getFileListActivity().requestDragAndDropPermissions(event);
        bRet = doDrop(event.getClipData(), position);
        if (bRet) {
            UiUtils.sendLocalBroadcastIntent(mContext, AppConstants.MYFILES_ACTION_DRAG_AND_DROP_FINISH, -1);
        }
        return bRet;
    }

    @Override
    protected FileRecord getDstFile(int dropPosition) {
        FileRecord dstFile = null;
        NavigationManager naviMgr = NavigationManager.getInstance(mFragment.getProcessId());
        if (dropPosition == AppConstants.UNDEFINED) {
            NavigationInfo curInfo = naviMgr.getCurInfo();
            if (curInfo != null) {
                dstFile = curInfo.getCurRecord();
            }
        } else {
            String fullPath = mAdapter.getFileRecord(dropPosition).getFullPath();
            dstFile = FileRecord.createFileRecord(FileRecord.StorageType.Local, fullPath);
            if (dstFile != null && !dstFile.isDirectory()) {
                dstFile = dstFile.getParent();
            }
        }
        return dstFile;
    }
}
